package com.example.fj2889.yuedongplayer;


import android.app.TabActivity;

import android.content.Intent;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.ParcelFileDescriptor;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;

import java.io.FileDescriptor;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends TabActivity{
    private List<Music> musicList=new ArrayList<Music>();
    private MediaPlayer mediaPlayer=new MediaPlayer();
    private Button previous_music;
    private Button play_music;
    private Button next_music;
    private TextView text_music_name;
    private TextView text_music_time;
    private int current_position=0;
    public ScanMusicList scanMusicList=new ScanMusicList();
    public ScanVedioList scanVedioList=new ScanVedioList();
    private int MusicNumber;

    private List<Video> videoList=new ArrayList<Video>();
    //////////////////////////////////////////////////////////////////////////////////////////////////////////

    ////////////////////////////////////////////////////////////////////////////////////////////////////

    //private ArrayList<Integer> list=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //TabHost tabHost = (TabHost)findViewById(R.id.tabhost);
        TabHost tabHost = getTabHost();
        TabHost.TabSpec page1 = tabHost.newTabSpec("tab1")
                .setIndicator("Music")
                .setContent(R.id.Music);
        tabHost.addTab(page1);
        TabHost.TabSpec page2 = tabHost.newTabSpec("tab2")
                .setIndicator("Video")
                .setContent(R.id.Video);
        tabHost.addTab(page2);



//        for (int i=0;i<scanMusicList.list.size();i++){
//            Log.d("MainActivity",scanMusicList.list.get(i));
//        }


        initMediaPlayer();

//        final ArrayList<Integer> raw_list = new ArrayList<Integer>();
//        final Field[] fields = R.raw.class.getFields();
//        for (int i = 0; i < fields.length; i++) {
//            try {
//                raw_list.add(fields[i].getInt(R.raw.class));
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }

        try {
            mediaPlayer.reset();
            mediaPlayer.setDataSource(ScanMusicList.list.get(0));
            mediaPlayer.prepare();

        } catch (IOException e) {
            e.printStackTrace();
        }

        previous_music=(Button)findViewById(R.id.previous_music);
        play_music=(Button)findViewById(R.id.play_music);
        next_music=(Button)findViewById(R.id.next_music);
        text_music_name=(TextView)findViewById(R.id.text_music_name);
        text_music_time=(TextView)findViewById(R.id.text_music_time);

        int x=ScanMusicList.list.get(current_position).split("/").length;
        text_music_name.setText(ScanMusicList.list.get(current_position).split("/")[x-1]);
        text_music_time.setText("Time "+String.valueOf(mediaPlayer.getDuration()/1000/60)+":"+String.valueOf(mediaPlayer.getDuration()/1000%60));


        initMusic();
        MusicAdapter adapter=new MusicAdapter(MainActivity.this,R.layout.music_item,musicList);
        ListView listView=(ListView) findViewById(R.id.music_list);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                current_position=i;
                play_music.setBackgroundResource(R.drawable.pause);

                if (mediaPlayer.isPlaying()) {
                    mediaPlayer.stop();
                }
                try {
                    mediaPlayer.reset();
                    mediaPlayer.setDataSource(ScanMusicList.list.get(current_position));
                    mediaPlayer.prepare();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                mediaPlayer.start();
                int x=ScanMusicList.list.get(current_position).split("/").length;
                text_music_name.setText(ScanMusicList.list.get(current_position).split("/")[x-1]);
                text_music_time.setText("Time "+String.valueOf(mediaPlayer.getDuration()/1000/60)+":"+String.valueOf(mediaPlayer.getDuration()/1000%60));

            }
        });

        previous_music.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mediaPlayer.isPlaying()) {
                    if (current_position>0) {
                        mediaPlayer.stop();
                        try {
                            mediaPlayer.reset();
                            mediaPlayer.setDataSource(ScanMusicList.list.get(current_position));
                            mediaPlayer.prepare();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        current_position-=1;
                        mediaPlayer.start();
                        int x=ScanMusicList.list.get(current_position).split("/").length;
                        text_music_name.setText(ScanMusicList.list.get(current_position).split("/")[x-1]);
                        text_music_time.setText("Time "+String.valueOf(mediaPlayer.getDuration()/1000/60)+":"+String.valueOf(mediaPlayer.getDuration()/1000%60));
                    }
                    else{
                        Toast.makeText(MainActivity.this,"This is the first music!",Toast.LENGTH_SHORT).show();
                    }
                }

            }
        });
        play_music.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mediaPlayer.isPlaying()){
                    mediaPlayer.pause();
                    play_music.setBackgroundResource(R.drawable.play);
                }
                else {
                    mediaPlayer.start();
                    play_music.setBackgroundResource(R.drawable.pause);
                }
            }
        });
        next_music.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mediaPlayer.isPlaying()){
                    if (current_position<ScanMusicList.list.size()-1) {
                        mediaPlayer.stop();
                        try {
                            mediaPlayer.reset();
                            mediaPlayer.setDataSource(ScanMusicList.list.get(current_position));
                            mediaPlayer.prepare();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        current_position+=1;
                        mediaPlayer.start();
                        int x=ScanMusicList.list.get(current_position).split("/").length;
                        text_music_name.setText(ScanMusicList.list.get(current_position).split("/")[x-1]);
                        text_music_time.setText("Time "+String.valueOf(mediaPlayer.getDuration()/1000/60)+":"+String.valueOf(mediaPlayer.getDuration()/1000%60));
                    }
                    else{
                        Toast.makeText(MainActivity.this,"This is the last music!",Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });


        initVideos();
        VideoAdapter videoAdapter=new VideoAdapter(MainActivity.this,R.layout.video_item,videoList);
        ListView video_listView=(ListView)findViewById(R.id.video_list);
        video_listView.setAdapter(videoAdapter);

        video_listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (mediaPlayer.isPlaying()) {
                    mediaPlayer.stop();
                }
                Intent intent=new Intent(MainActivity.this,VideoActivity.class);
                intent.putExtra("address",ScanVedioList.list.get(i));
                startActivity(intent);

            }
        });

    }

    ///////////////////////////////////////////////////////////////////////////////////////////////////////////

    /////////////////////////////////////////////////////////////////////////////////////////////////////

    private void initMusic(){
        int i;
        for (i=0;i<ScanMusicList.list.size();i++) {
            int x=ScanMusicList.list.get(i).split("/").length;
            String address=ScanMusicList.list.get(i);
            Music music = new Music(ScanMusicList.list.get(i).split("/")[x-1],1);
            musicList.add(music);
        }
    }

    private void initVideos(){
        int i;
        for (i=0;i<ScanVedioList.list.size();i++) {
            int x=ScanVedioList.list.get(i).split("/").length;
            Video video=new Video(ScanVedioList.list.get(i).split("/")[x-1],1);
            videoList.add(video);
        }
    }

    private void initMediaPlayer(){
        try{
            //File file=new File(Environment.getExternalStorageDirectory(),"Cannon.mp3");
            //mediaPlayer.setDataSource(file.getPath());
            //mediaPlayer.prepare();
            //mediaPlayer.start();
            //mediaPlayer.prepare();
        }catch (Exception e){
            Log.d("MainActivity","Cannot find the file!");
        }
    }


}
